package com.moyq5.mvc.maker.test.bean.entity;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;

import com.moyq5.mvc.maker.test.bean.custom.QueryEntity;
import com.moyq5.mvc.maker.test.bean.enums.RefundRefType;
import com.moyq5.mvc.maker.test.bean.enums.RefundStatus;
import com.moyq5.mvc.maker.test.bean.enums.RefundType;
import com.moyq5.mvc.maker.test.bean.enums.SysOperatorType;

/**
 * 退款订单
 * @author Moyq5
 * @date 2018年6月30日
 */
@Entity
@Table(name = "tra_refund")
@DynamicUpdate
public class TraRefund extends QueryEntity implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	/**
	 * 退款单ID
	 */
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")@GenericGenerator(name = "native", strategy = "native")
    @Column(name = "refund_id")
	private Long refundId;
	/**
	 * 退款流水号（平台单号）
	 */
	@Column(name = "refund_no", length = 30, nullable = false)
	private String refundNo;
	/**
	 * 原支付单号（平台单号）
	 */
	@Column(name = "order_no", length = 30, nullable = false)
	private String orderNo;
	/**
	 * 原渠道支付单号
	 */
	@Column(name = "channel_order_no", length = 30, nullable = false)
	private String channelOrderNo;
	/**
	 * 原支付单金额（元）
	 */
	@Column(name = "order_amount", precision = 10, scale = 2, nullable = false)
	private BigDecimal orderAmount;
	/**
	 * 申请退款金额（元）
	 */
	@Column(name = "amount", precision = 10, scale = 2, nullable = false)
	private BigDecimal amount;
	/**
	 * 实际退款金额（元）
	 */
	@Column(name = "refund_amount", precision = 10, scale = 2, nullable = false)
	private BigDecimal refundAmount;
	/**
	 * 退款方式
	 */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "type", precision = 2, nullable = false)
	private RefundType type;
    /**
	 * 退款（备注）说明
	 */
	@Column(name = "remark", length = 300)
	private String remark;
	/**
	 * 扣款账户
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "virtual_account_id", nullable = false)
	private VirtualAccount virtualAccount;
	/**
	 * 收款账户
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "account_id")
	private TraAccount account;
	/**
	 * 操作人类型
	 */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "operator_type", precision = 2, nullable = false)
	private SysOperatorType operatorType;
    /**
	 * 操作人（管理员）
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "admin_id")
	private SysAdmin operatorAdmin;
	/**
	 * 退款状态
	 */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "status", precision = 2, nullable = false)
	private RefundStatus status;
	/**
	 * 退款状态描述
	 */
	@Column(name = "status_desc", length = 300)
	private String statusDesc;
	/**
	 * 退款完成时间
	 */
	@Column(name = "refund_time", columnDefinition="datetime")
	private Date refundTime;
	/**
	 * 退款渠道
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "channel_order_id")
	private TraChannelOrder channelOrder;
	/**
	 * 退款来源类型
	 */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "ref_type", precision = 2, nullable = false)
	private RefundRefType refType;
	/**
	 * (来源)用户订单
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "user_order_id")
	private UserOrder userOrder;
	/**
	 * 添加时间（申请退款时间）
	 */
	@Column(name = "add_time", nullable = false, columnDefinition="datetime")
	private Date addTime;
	
	@PrePersist
	public void prePersist() {
		super.prePersist();
		if (null == addTime) {
			addTime = new Date();
		}
		if (null == refundNo) {
			refundNo = "8" + orderNo.substring(orderNo.length()-9) 
					+ new SimpleDateFormat("yyyyMMddHHmm").format(new Date()) 
					+ Math.round(Math.random() * (1e6-1e5-1) + 1e5);
		}
		
	}

	public Long getRefundId() {
		return refundId;
	}

	public void setRefundId(Long refundId) {
		this.refundId = refundId;
	}

	public String getRefundNo() {
		return refundNo;
	}

	public void setRefundNo(String refundNo) {
		this.refundNo = refundNo;
	}

	public String getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}

	public String getChannelOrderNo() {
		return channelOrderNo;
	}

	public void setChannelOrderNo(String channelOrderNo) {
		this.channelOrderNo = channelOrderNo;
	}

	public BigDecimal getAmount() {
		return amount;
	}

	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	public BigDecimal getRefundAmount() {
		return refundAmount;
	}

	public void setRefundAmount(BigDecimal refundAmount) {
		this.refundAmount = refundAmount;
	}

	public RefundType getType() {
		return type;
	}

	public void setType(RefundType type) {
		this.type = type;
	}

	public VirtualAccount getVirtualAccount() {
		return virtualAccount;
	}

	public void setVirtualAccount(VirtualAccount virtualAccount) {
		this.virtualAccount = virtualAccount;
	}

	public TraAccount getAccount() {
		return account;
	}

	public void setAccount(TraAccount account) {
		this.account = account;
	}

	public SysOperatorType getOperatorType() {
		return operatorType;
	}

	public void setOperatorType(SysOperatorType operatorType) {
		this.operatorType = operatorType;
	}

	public SysAdmin getOperatorAdmin() {
		return operatorAdmin;
	}

	public void setOperatorAdmin(SysAdmin operatorAdmin) {
		this.operatorAdmin = operatorAdmin;
	}

	public RefundStatus getStatus() {
		return status;
	}

	public void setStatus(RefundStatus status) {
		this.status = status;
	}

	public String getStatusDesc() {
		return statusDesc;
	}

	public void setStatusDesc(String statusDesc) {
		this.statusDesc = statusDesc;
	}

	public Date getRefundTime() {
		return refundTime;
	}

	public void setRefundTime(Date refundTime) {
		this.refundTime = refundTime;
	}

	public TraChannelOrder getChannelOrder() {
		return channelOrder;
	}

	public void setChannelOrder(TraChannelOrder channelOrder) {
		this.channelOrder = channelOrder;
	}

	public Date getAddTime() {
		return addTime;
	}

	public void setAddTime(Date addTime) {
		this.addTime = addTime;
	}

	public RefundRefType getRefType() {
		return refType;
	}

	public void setRefType(RefundRefType refType) {
		this.refType = refType;
	}

	public UserOrder getUserOrder() {
		return userOrder;
	}

	public void setUserOrder(UserOrder userOrder) {
		this.userOrder = userOrder;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public BigDecimal getOrderAmount() {
		return orderAmount;
	}

	public void setOrderAmount(BigDecimal orderAmount) {
		this.orderAmount = orderAmount;
	}
	
}
